<?php

function LoadRateVCB()
{
    ImportClass('json');
    
    $content   = getConfig('RateVCB',"",true);

    if($content==false)
    {
        return;
    }
    $content   = unserialize($content);
    return $content;
}

function updateRateVCB($link="http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx")
{
    global $dbsql;
    ImportClass('json');
    
	$content   = file_get_contents($link);

	if(strlen($content)>300)
	{	
        $rateobj   = array();
        
		$x         = new SimpleXmlElement($content);
		foreach($x->Exrate  as $entry) 
        {
			if($entry['Buy']==0)continue;            
			$entry['Buy']    = preg_replace("/(\d+)(\d{3})(.?\d*)/","$1,$2$3",$entry['Buy']);
            
            $obj             = array('name'=>$entry['CurrencyName']."",'buy'=>$entry['Buy']."");
			$rateobj[$entry['CurrencyCode'].'']       = $obj;
		}
        $content    = serialize($rateobj);
        
        $isExists   = getConfig('RateVCB',"",true);
        if($isExists)
        {
            $sql_query = "UPDATE tblconfig set cValue='$content',cType='".time()."' where cName='RateVCB'";
        }else
        {
            $sql_query = "insert into tblconfig(cName,cValue,cType) values('RateVCB','$content','".time()."')";
        }
		
		$rs = $dbsql->query($sql_query);
        if($rs)
        {
            return true;
        }
	}
    return false;
}

function updateOil()
{
    global $dbsql,$language;
    $data   = @file_get_contents('http://www.oil-price.net/TABLE2/gen.php?lang=en');
    if(empty($data))
    {
        return;
    }
    $data       = str_replace('font-size: 12px','font-size: 11px',$data);
    $data       = str_replace('font-size: 18px','font-size: 14px',$data);
    $data       = str_replace('font-size: 16px','font-size: 13px',$data);
    $data       = str_replace('font-size: 22px','font-size: 14px',$data);
    $data       = str_replace('; border: 1px solid #000000',';width:100%',$data);
    $data       = str_replace('new Image(200,100);','new Image(175,80);',$data);
    $data       = str_replace('border=0>','width=175 border=0>',$data);
    $data       = str_replace('colspan=2','colspan=4',$data);
    $data       = str_replace('http://oil-price.net/dashboard.php?lang=en','http://oil-price.net/',$data);
    $data       = str_replace('.png\" border=0 id=\"button','.png\" border=0 width=35 id=\"button',$data);
    $data       = str_replace('font-family: Arial, Helvetica, sans-serif;','',$data);
    $data       = mysql_escape_string($data);
    saveConfig('CrudeOil',$data);
}

function updateNatuGas()
{
    $data   = @file_get_contents('http://www.oil-price.net/widgets/natural_gas_large/gen.php?lang=en#natural_gas_large');
    if(empty($data))
    {
        return;
    }
    $data       = str_replace('font-size: 12px','font-size: 11px',$data);
    $data       = str_replace('font-size: 18px','font-size: 14px',$data);
    $data       = str_replace('font-size: 16px','font-size: 13px',$data);
    $data       = str_replace('font-size: 22px','font-size: 14px',$data);
    $data       = str_replace('; border: 1px solid #000000',';width:100%',$data);
    $data       = str_replace('new Image(200,100);','new Image(175,80);',$data);
    $data       = str_replace('id=\"large_ng_graph\"','width=175 id=\"large_ng_graph\"',$data);
    $data       = str_replace('colspan=2','colspan=4',$data);
    
    $data       = str_replace('.png\" id=\"large_ng_button','.png\" border=0 width=35 id=\"large_ng_button',$data);
    $data       = str_replace('font-family: Arial, Helvetica, sans-serif;','',$data);
    $data       = mysql_escape_string($data);
    saveConfig('NaturalGas',$data);
}
?>